// src/router/index.ts
import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { useCustomerStore } from '@/stores/customer'

const routes = [
  /* 1. 登录页 */
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue')
  },

  /* 2. 员工端仪表盘 ---------------------------------------------------- */
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: () => import('@/views/Dashboard.vue'),
    redirect: '/dashboard/report',
    meta: { requiresAuth: true },     // 需要登录
    children: [
      { path: 'tables', component: () => import('@/views/Tables.vue') },
      { path: 'dishes', component: () => import('@/views/Dishes.vue') },
      { path: 'stock',  component: () => import('@/views/Empty.vue') },
      { path: 'users',  component: () => import('@/views/UserList.vue') },
      { path: 'report', component: () => import('@/views/Report.vue') },
      { path: 'Order', component: () => import('@/views/Order.vue') },
      {path: 'profile', component: () => import('@/views/Profile.vue') }
      
    ]
  },

  /* 3. 管理员端 --------------------------------------------------------- */
  {
    path: '/admin',
    name: 'AdminLayout',
    component: () => import('@/views/admin/AdminLayout.vue'),
    redirect: '/admin/stores',
    meta: { requiresAuth: true, role: 'ADMIN' },
    children: [
      { path: 'stores',   component: () => import('@/views/admin/Stores.vue') },
      { path: 'waiters',  component: () => import('@/views/admin/Waiters.vue') },
      { path: 'printers', component: () => import('@/views/admin/PrinterView.vue') },
      { path: 'pay',      component: () => import('@/views/admin/Empty.vue') }
    ]
  },

  /* 4. 顾客端 ----------------------------------------------------------- */
  {
  path: '/select-store',
  name: 'SelectStore',
  component: () => import('@/views/customer/SelectStore.vue'),
  meta: { requiresAuth: true, role: 'CUSTOMER' }
},
  
  {
    path: '/customer',
    name: 'CustomerLayout',
    component: () => import('@/views/customer/customer.vue'),
    redirect: '/customer/order',
    meta: { requiresAuth: true },
    children: [
      { path: 'store-detail', component: () => import('@/views/customer/StoreDetail.vue') },
      { path: 'order',        component: () => import('@/views/customer/OrderMenu.vue') },
      { path: 'my-order',     component: () => import('@/views/customer/MyOrder.vue') },
      { path: 'pay/:orderId', name: 'Pay', component: () => import('@/views/customer/Pay.vue'), props: true },
      {path: 'profile', component: () => import('@/views/customer/Profile.vue') }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

/* 全局守卫：仅校验登录态，如需按角色拦截请在各 layout 内再判断 */
/* 全局守卫：1) 登录态  2) 顾客必选房 */
router.beforeEach((to, from, next) => {
  const auth = useAuthStore()
  const customer = useCustomerStore()

  /* ① 登录校验 */
  if (to.meta.requiresAuth && !auth.token) {
    return next('/login')
  }

  /* ② 顾客选房拦截（优先级在主页之上） */
  if (to.path.startsWith('/customer') &&
      to.name !== 'SelectStore' &&          // 避免死循环
      auth.token &&
      auth.payload?.role === 1) {           // 1 = 顾客
    customer.loadStore()                    // 同步缓存
    if (!customer.store?.id) {
      // 把想去的地方记下来，选完直接跳回
      return next({ name: 'SelectStore', query: { redirect: to.fullPath } })
    }
  }

  next()
})
export default router